package com.heima.wemedia.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.vo.WmUserLoginVo;
import com.heima.model.wemedia.dtos.WmLoginDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.common.BCrypt;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.springframework.stereotype.Service;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult login(WmLoginDto dto) {
       //TODO
        LambdaQueryWrapper<WmUser> queryWrapper = Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, dto.getName());
        WmUser wmUser = this.getOne(queryWrapper);
        if(ObjectUtil.isEmpty(wmUser)){
            return ResponseResult.errorResult(1002,"用户不存在");
        }else {
            boolean checkpw = BCrypt.checkpw(dto.getPassword(), wmUser.getPassword());
            if(!checkpw){
                return ResponseResult.errorResult(2,"密码错误");
            }
            String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
            WmUserLoginVo wmUserLoginVo = new WmUserLoginVo(wmUser, token);
            return ResponseResult.okResult(wmUserLoginVo);
        }
    }
}